package com.graduation.controller;

import com.graduation.entity.Shop;
import com.graduation.entity.User;
import com.graduation.exception.ShopIsExitsException;
import com.graduation.service.ShopService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PageDispatcherController {
    @Resource
    private ShopService shopService;

    @GetMapping("/dispatcher")
    public String dispatcherPage(HttpServletRequest request) {
        //是否登录校验
        HttpSession session = request.getSession(false);
        if (session == null) {
            return "login-register";
        }
        //获取页面路径
        String attr = request.getParameter("url");
        //截取掉后缀名称
        boolean isHtml = attr.endsWith(".html");
        StringBuilder sb = new StringBuilder();
        if (isHtml) {
            //分割完成之后返回的字符串
            String[] split = attr.split(".html");
            for (int i = 0; i < split.length; i++) {
                sb.append(split[i]);
            }
        }
        return sb.toString();
    }

    /**
     * 商家入住
     *
     * @param request
     * @return
     */
    @RequestMapping("/join.do")
    public ModelAndView businessJoin(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        try {
            Shop shop = new Shop();
            shop.setState("1");
            shop.setGender("1");
            shop.setCountry("1");
            shop.setEmail(user.getEmail());
            shopService.insertEmailToShop(shop);
        } catch (Exception e) {
            System.out.println("店铺已经存在@");
        }
        mv.addObject("email", user.getEmail());
        mv.setViewName("checkout");
        return mv;
    }

    //转发到商品发布页面
    @GetMapping("/toGoodsAdd.do")
    public ModelAndView toGoodsPages(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        //拿到当前的session值
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        if (user != null) {
            Integer res = shopService.queryShopId(user.getEmail());
            if (res != null) {
                mv.addObject("id", res);
                mv.setViewName("goodsAdd");
                return mv;
            } else {
                mv.addObject("isNotShop", "(您还不是商家!)");
                mv.setViewName("index");
                return mv;
            }
        }
        //返回首页显示信息
        mv.addObject("isNotShop", "(您尚未登录!)");
        mv.setViewName("index");
        return mv;
    }

    //转发单个登录页面
    @GetMapping("/login.do")
    public String turnToLoginAndRegister() {
        return "login-register";
    }
}
